import { createRouter, createWebHashHistory } from "vue-router"
import Home from "@/views/Home.vue"

const asyncRoutes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: {
      title: "首页",
      isShowNav: true,
    },
  },
  {
    path: "/category",
    name: "Category",
    component: () => import("@/views/Category.vue"),
    meta: {
      title: "分类",
      isShowNav: true,
    },
  },
  {
    path: "/cart",
    name: "Cart",
    component: () => import("@/views/Cart.vue"),
    meta: {
      title: "购物车",
      isShowNav: true,
    },
  },
  {
    path: "/user",
    name: "User",
    component: () => import("@/views/User.vue"),
    meta: {
      title: "我的",
      isShowNav: true,
    },
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/Login.vue"),
    meta: {
      title: "登录",
      isShowNav: false,
    },
  },
  {
    path: "/detail/:id",
    name: "Detail",
    component: () => import("@/views/Detail.vue"),
    meta: {
      title: "详情",
      isShowNav: false,
    },
  },
  {
    path: "/addressList",
    name: "AddressList",
    component: () => import("@/views/AddressList.vue"),
    meta: {
      title: "地址列表",
      isShowNav: false,
    },
  },
  {
    path: "/addressManage",
    name: "AddressManage",
    component: () => import("@/views/AddressManage.vue"),
    meta: {
      title: "地址管理",
      isShowNav: false,
    },
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    ...asyncRoutes,
    {
      path: "/:pathMatch(.*)*",
      name: "NotFound",
      component: () => import("@/views/NotFound.vue"),
      meta: {
        title: "404",
        isShowNav: false,
      },
    },
  ],
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || "新峰商城"
  next()
})

export default router
